<template>
  <div class="main">
    <div class="head-tool">
      <div class="title"> {{ title }} </div>
      <div class="action">
        <a-button type="primary" icon="file-done" shape="round" @click="toPrint">打印</a-button>
        <a-divider type="vertical" />
        <a-button type="primary" icon="download" shape="round" @click="downloadFile">下载</a-button>
      </div>
    </div>
    <div class="pdf-view">
      <pdf
        ref="pdf"
        v-if="pdfSrc"
        :src="pdfSrc"
        v-for="i in numPages"
        :key="i"
        :page="i"/>
    </div>
  </div>
</template>
<script>
import pdf from 'vue-pdf'
export default {
  components: {
    pdf
  },
  props: {
    src: {
      type: String,
      default: ''
    },
    title: {
      type: String,
      default: 'PDF文件'
    }
  },
  data () {
    return {
      pdfSrc: '',
      numPages: undefined
    }
  },
  mounted () {
    const loadingTask = pdf.createLoadingTask(this.src)
    this.pdfSrc = loadingTask
    this.pdfSrc.promise.then(pdf => {
      this.numPages = pdf.numPages
    })
  },
  methods: {
    // 打印
    toPrint () {
      this.$refs.pdf[0].print()
    },
    // 下载
    downloadFile () {
      // 后台请求流
      // 通过a标签下载
      // const link = document.createElement('a')
      // link.setAttribute('href', this.$props.src)
      // link.click() // // 自执行点击事件
      // document.body.removeChild(link)
      // 通过open方式
      // window.open( this.$props.src)
    }
  }
}
</script>
<style lang="less">
  .main{
    height: 100%;
    width: 100%;
    .head-tool{
      height: 50px;
      line-height: 50px;
      position: relative;
      border-bottom: 1px solid #e8e8e8;
      margin-bottom: -1px;
      .title{
        float: left;
        width: 120px;
        font-weight: 600;
        color: rgba(0, 0, 0, 0.85);
        font-size: 16px;
      }
      .action{
        float: right;
      }
    }
    .pdf-view {
      margin-top: 1px;
      height: calc(100% - 50px);
      overflow-y: auto;
    }
  }
</style>
